package com.stylefeng.guns.modular.pay.service.impl;

import com.stylefeng.guns.modular.pay.model.AllinMch;
import com.stylefeng.guns.modular.pay.model.WithdrawOrder;
import com.stylefeng.guns.modular.pay.model.WithdrawtAccount;
import com.stylefeng.guns.modular.pay.payment.allin.common.api.ApiRsp;
import com.stylefeng.guns.modular.pay.payment.allin.common.api.RpcTransQueryReq;
import com.stylefeng.guns.modular.pay.dao.WithdrawOrderMapper;
import com.stylefeng.guns.modular.pay.service.AllInPayService;
import com.stylefeng.guns.modular.pay.service.IAllinMchService;
import com.stylefeng.guns.modular.pay.service.IWithdrawOrderService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.base.Strings;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 体现流水记录 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-09-11
 */
@Service
public class WithdrawOrderServiceImpl extends ServiceImpl<WithdrawOrderMapper, WithdrawOrder> implements IWithdrawOrderService {
	@Autowired
	private WithdrawOrderMapper withdrawOrderMapper;
	@Autowired
	private AllInPayService allInPayService;
	@Autowired
	private IAllinMchService allinMchService;
	
	@Override
	public List<WithdrawOrder> selectList(WithdrawOrder order) {
		EntityWrapper<WithdrawOrder> wrapper = new EntityWrapper<>();
		if(!Strings.isNullOrEmpty(order.getOutTradeNo())){
			wrapper.eq("out_trade_no", order.getOutTradeNo());
		}
		if(!Strings.isNullOrEmpty(order.getAccountName())){
			wrapper.eq("account_name", order.getAccountName());
		}
		if(!Strings.isNullOrEmpty(order.getAccountNo())){
			wrapper.and().eq("account_no", order.getAccountNo());
		}
		if(order.getStatus()!=null){
			wrapper.and().eq("status", order.getStatus());
		}
		wrapper.orderBy("id", false);
		
		return withdrawOrderMapper.selectList(wrapper);
	}
	
	
	@Override
	public void syn(Integer withdrawOrderId) {
		WithdrawOrder order = withdrawOrderMapper.selectById(withdrawOrderId);
		if(order==null){
			throw new RuntimeException("订单不存在");
		}
		//配置信息
		AllinMch mch = (AllinMch) allinMchService.selectObj(new EntityWrapper<AllinMch>().eq("status", "1"));
		if(mch==null){
			throw new RuntimeException("渠道配置信息尚未配置");
		}
		RpcTransQueryReq req = new RpcTransQueryReq();
		req.setStatus("2");//全部
		req.setQuerySn(order.getPlatTradeNo());
		req.setType("1");//0.按完成日期1.按提交日期，默认为1
		ApiRsp res = null;
		try {
			 res = allInPayService.queryTranx(req, mch);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
		String RET_CODE = res.getINFO().getRET_CODE();
		
	
		
		
		
	}

}
